package CAD;

import j3d.J3D;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GraphicsConfiguration;
import java.awt.MenuItem;
import java.awt.color.ColorSpace;
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener;
import java.awt.peer.MenuItemPeer;
import java.util.ArrayList;
import java.util.Vector;
import javax.media.j3d.Alpha;
import javax.media.j3d.Appearance;
import javax.media.j3d.BoundingSphere;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Canvas3D;
import javax.media.j3d.Material;
import javax.media.j3d.PolygonAttributes;
import javax.media.j3d.RotationInterpolator;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JTree;
import javax.swing.ToolTipManager;
import javax.vecmath.Point3d;
import operationSolids.CSGSolid;
import operationSolids.CompoundSolid;
import project.Project;
import com.sun.j3d.utils.behaviors.mouse.MouseRotate;
import com.sun.j3d.utils.geometry.Box;
import com.sun.j3d.utils.geometry.ColorCube;
import com.sun.j3d.utils.universe.SimpleUniverse;
import com.sun.java.swing.plaf.nimbus.MenuItemPainter;

import dataBase.ProjectBD;
import features.Block;
import features.Feature;
import featuresWindows.CreateBezier;
import featuresWindows.CreateConicalHoleBottom;
import featuresWindows.CreateHole;
import featuresWindows.CreatePlanarFace;
import featuresWindows.CreatePocket;
import featuresWindows.CreateRoundFloorHole;
import featuresWindows.CreateRoundFloorPocket;
import featuresWindows.CreateSlot;
import featuresWindows.CreateSphericalHoleBottom;
import featuresWindows.CreateStep;




import visualCAD.*;

public class MainFrame extends MainWindow implements ActionListener{
	
// <atributos> -------------------
	
	public static J3D j3d = null;
	private NewProject nMenu;
	static public Block block=null;
	static public FeaturesTree FTFeature;
	static public BlockTree FTBlock;
	static public  ArrayList<Feature> features  = new ArrayList();
	static JPanel p12 = new JPanel();
	static public Project project = new Project();
	//<appearance>
	public static int appearance=0;
	public final static int PAINTED=0;
	public final static int LINE=1;
	//</appearance>
	public static int Face=1;
	//<faces> -------------------
	public final static int TOPFACE = 1;
	public final static int FRONTFACE = 2;
	public final static int WESTFACE = 3;
	public final static int BACKFACE = 4;
	public final static int EASTFACE = 5;
	public final static int LOWERFACE = 6;
	//</faces> -------------------
	//</atributos>
	//
	public MainFrame(){
		super();
		
		//<face buttons>
		ButtonGroup bgroup = new ButtonGroup();
		bgroup.add(leftButton);
		bgroup.add(rightButton);
		bgroup.add(topButton);
		bgroup.add(bottonButton);
		bgroup.add(frontButton);
		bgroup.add(backButton);
		//</face buttons>

		//<face buttons Menu>
		ButtonGroup bgroup2 = new ButtonGroup();
		bgroup2.add(leftViewMenuItem);
		bgroup2.add(rightViewMenuItem);
		bgroup2.add(topViewMenuItem);
		bgroup2.add(bottomViewMenuItem);
		bgroup2.add(frontViewMenuItem);
		bgroup2.add(backViewMenuItem);


		j3d= new J3D(this.panel1);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		initListeners();
		setVisible(true);
		initJTree();

	}
	//</construtores> -------------------
	//<metodos>
	public static J3D getJ3d() {
		return j3d;
	}
	public void setBlock(Block block) {
		this.block = block;
	}
	void setIconsEnable(){
		addFeatureMenu.setEnabled(true);
		removeFeatureMenuItem.setEnabled(true);
		stepNCCodeMenuItem.setEnabled(true);
		holeRootMenu.setEnabled(true);
		pocketRootMenu.setEnabled(true);
		bezierPathMenuItem.setEnabled(true);
		roundFloorHoleMenuItem.setEnabled(true);
		roundFloorPocketMenu.setEnabled(true);
		leftButton.setEnabled(true);
		leftViewMenuItem.setEnabled(true);
		topButton.setEnabled(true);
		topViewMenuItem.setEnabled(true);
		bottomViewMenuItem.setEnabled(true);
		bottonButton.setEnabled(true);
		rightButton.setEnabled(true);
		rightViewMenuItem.setEnabled(true);
		backButton.setEnabled(true);
		backViewMenuItem.setEnabled(true);
		frontButton.setEnabled(true);
		frontViewMenuItem.setEnabled(true);
		slotRootMenu.setEnabled(true);
		holeMenuItem.setEnabled(true);
		stepMenuItem.setEnabled(true);
		pocketMenu.setEnabled(true);
		slotMenuItem.setEnabled(true);
		wireframeViewMenuItem.setEnabled(true);
		solidViewMenuItem.setEnabled(true);
		roundFloorPocketMenu.setEnabled(true);
		saveDBButton.setEnabled(true);
		saveInServer.setEnabled(true);
		bezierPathMenuItem.setEnabled(true);
		planarFaceMenuItem.setEnabled(true);
		this.conicalHoleBottom.setEnabled(true);
		this.sphericalHoleBottom.setEnabled(true);
	}
	void initJTree(){
		p12=new JPanel();
		FTFeature=new FeaturesTree();
		//	FTBlock=new BlockTree();
		p12.setLayout(new BorderLayout());
		p12.setBackground(Color.WHITE);
		p12.setMinimumSize(new Dimension(200,0));
		splitPane1.setLeftComponent(p12);
		p12.add(FTFeature.getTree(),BorderLayout.NORTH);
		//		p12.add(FTBlock.getTree(),BorderLayout.SOUTH);
	}
	public static void refreshJTree(){
		p12=new JPanel();
		FTFeature=new FeaturesTree();
		FTBlock=new BlockTree();
		p12.setLayout(new BorderLayout());
		p12.setBackground(Color.WHITE);
		p12.setMinimumSize(new Dimension(200,0));

		splitPane1.setLeftComponent(p12);
		p12.add(FTFeature.getTree(),BorderLayout.NORTH);
		p12.add(FTBlock.getTree(),BorderLayout.CENTER);
	}
	public void initListeners(){


		this.backButton.addActionListener(this);
		this.backViewMenuItem.addActionListener(this);
		this.bottomViewMenuItem.addActionListener(this);
		this.bottonButton.addActionListener(this);
		this.frontButton.addActionListener(this);
		this.frontViewMenuItem.addActionListener(this);
		this.holeMenuItem.addActionListener(this);
		this.leftButton.addActionListener(this);
		this.leftViewMenuItem.addActionListener(this);
		this.menuAbout.addActionListener(this);
		this.menuHelp.addActionListener(this);
		this.newButton.addActionListener(this);
		this.newMenu.addActionListener(this);
		this.openButton.addActionListener(this);
		this.openInServer.addActionListener(this);
		this.openLocal.addActionListener(this);
		this.pocketMenu.addActionListener(this);
		this.printButton.addActionListener(this);
		this.printMenu.addActionListener(this);
		this.rightButton.addActionListener(this);
		this.rightViewMenuItem.addActionListener(this);
		this.saveButton.addActionListener(this);
		this.saveInServer.addActionListener(this);
		this.saveLocal.addActionListener(this);
		this.sendOrder.addActionListener(this);
		this.solidViewMenuItem.addActionListener(this);
		this.slotMenuItem.addActionListener(this);
		this.stepMenuItem.addActionListener(this);
		this.topButton.addActionListener(this);
		this.topViewMenuItem.addActionListener(this);
		this.sairMenuItem.addActionListener(this);
		this.wireframeViewMenuItem.addActionListener(this);
		this.roundFloorPocketMenu.addActionListener(this);
		this.roundFloorHoleMenuItem.addActionListener(this);
		this.removeFeatureMenuItem.addActionListener(this);
		this.stepNCCodeMenuItem.addActionListener(this);
		this.saveDBButton.addActionListener(this);
		this.saveInServer.addActionListener(this);
		this.bezierPathMenuItem.addActionListener(this);
		this.planarFaceMenuItem.addActionListener(this);
		this.conicalHoleBottom.addActionListener(this);
		this.sphericalHoleBottom.addActionListener(this);
		
	}
	public void actionPerformed(ActionEvent evt) {
		Object source = evt.getSource();

		if(source.equals(this.newButton)){
			if(block==null){
				nMenu = new NewProject(this);

			}else{
				JOptionPane.showMessageDialog(null, "You have already created a block.");
			}
		}
		if(source.equals(this.sairMenuItem)){
			dispose();
		}
		if(source.equals(this.bezierPathMenuItem)){
			new CreateBezier(j3d,block);
		}
		if(source.equals(this.saveDBButton)||source.equals(this.saveInServer)){
			project.refresh();
			ProjectBD.getInstance().insert(project);
		}
		if(source.equals(this.planarFaceMenuItem)){
			new CreatePlanarFace(j3d,block);
		}
		if(source.equals(this.solidViewMenuItem)){
			if(appearance!=PAINTED){
				appearance=PAINTED;
				MainFrame.j3d.removeSolid();
				MainFrame.getJ3d().addSolid((new Block ("comp",CompoundSolid.UNION,MainFrame.block,MainFrame.block.block)).block);

			}


		}
		if(source.equals(this.wireframeViewMenuItem)){
			if(appearance!=LINE){
				appearance=LINE;
				MainFrame.j3d.removeSolid();
				MainFrame.getJ3d().addSolid((new Block ("comp",CompoundSolid.UNION,MainFrame.block,MainFrame.block.block)).block);
			}
		}
		if(source.equals(this.removeFeatureMenuItem)){
			new RemoveFeature(j3d); 
		}
		if(source.equals(this.newMenu)){
			if(block==null){
				nMenu = new NewProject(this);

			}else{
				JOptionPane.showMessageDialog(null, "You have already created a block.");
			}
		}
		if(source.equals(topButton)){
			block.rotateFaceXY(MainFrame.TOPFACE);
			topViewMenuItem.setSelected(true);
		}
		if(source.equals(rightButton)){
			block.rotateFaceXY(MainFrame.EASTFACE);
			rightViewMenuItem.setSelected(true);
		}
		if(source.equals(leftButton)){
			block.rotateFaceXY(MainFrame.WESTFACE);
			leftViewMenuItem.setSelected(true);
		}
		if(source.equals(bottonButton)){
			block.rotateFaceXY(MainFrame.LOWERFACE);
			bottomViewMenuItem.setSelected(true);
		}
		if(source.equals(backButton)){
			block.rotateFaceXY(MainFrame.BACKFACE);
			backViewMenuItem.setSelected(true);
		}
		if(source.equals(frontButton)){
			block.rotateFaceXY(MainFrame.FRONTFACE);
			frontViewMenuItem.setSelected(true);
		}
		if(source.equals(frontViewMenuItem)){
			block.rotateFaceXY(MainFrame.FRONTFACE);
			frontButton.setSelected(true);
		}
		if(source.equals(backViewMenuItem)){
			block.rotateFaceXY(MainFrame.BACKFACE);
			backButton.setSelected(true);
		}
		if(source.equals(topViewMenuItem)){
			block.rotateFaceXY(MainFrame.TOPFACE);
			topButton.setSelected(true);
		}
		if(source.equals(bottomViewMenuItem)){
			block.rotateFaceXY(MainFrame.LOWERFACE);
			bottonButton.setSelected(true);
		}
		if(source.equals(rightViewMenuItem)){
			block.rotateFaceXY(MainFrame.EASTFACE);
			rightButton.setSelected(true);
		}
		if(source.equals(leftViewMenuItem)){
			block.rotateFaceXY(MainFrame.WESTFACE);
			leftButton.setSelected(true);
		}
		if(source.equals(this.holeMenuItem)){

			CreateHole holeWindow = new CreateHole(j3d,block);
		}
		if(source.equals(this.roundFloorHoleMenuItem)){

			CreateRoundFloorHole roundFloorHoleWindow = new CreateRoundFloorHole(j3d,block);
		}
		if(source.equals(this.slotMenuItem)){

			CreateSlot sljn = new CreateSlot(this.j3d,this.block);
		}
		if(source.equals(this.stepMenuItem)){

			CreateStep stepWindow = new CreateStep(this.j3d,this.block);
		}
		if(source.equals(this.pocketMenu)){

			CreatePocket holeWindow = new CreatePocket(j3d,block);
		}
		if(source.equals(this.menuAbout))
		{
			About ab = new About();
		}
		if(source.equals(this.roundFloorPocketMenu))
		{
			CreateRoundFloorPocket crfpWindow = new CreateRoundFloorPocket(j3d, block);
		}
		if(source.equals(this.conicalHoleBottom))
		{
			CreateConicalHoleBottom conicalHoleBottom = new CreateConicalHoleBottom(j3d, block);
		}
		if(source.equals(this.sphericalHoleBottom))
		{
			CreateSphericalHoleBottom sphericalHoleBottom = new CreateSphericalHoleBottom(j3d, block);
		}
	}

}

